package com.joven.core.process;

import cn.hutool.core.collection.CollectionUtil;
import com.joven.core.constant.enums.BizMsgType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzj on 2021/10/17.
 *
 * @author joven
 */
@Component
@RequiredArgsConstructor
public class BizMsgProcessFactory implements InitializingBean {

    /**
     * 类型， 实现接口
     */
    Map<BizMsgType, BizMsgProcesser> processers;

    /**
     * spring 启动自动加载接口
     */
    private final List<BizMsgProcesser> handlers;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (CollectionUtil.isNotEmpty(handlers)) {
            processers = new HashMap<>(16);
            for (BizMsgProcesser handler : handlers) {
                processers.put(handler.getType(), handler);
            }
        }
    }

    /**
     * 统一工厂类处理
     * @param type 业务处理类型
     * @param job 处理入参值
     * @return 处理结果
     */
    public String process(BizMsgType type,Object job){
        return processers.get(type).doSomeThing(job);
    }

}
